﻿/*
 * Copyright (C) 2009-2010 Valerio Fuoglio <valerio.fuoglio@gmail.com>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
using Lastfm.Scrobbling;
using Lastfm.Services;

namespace LightScrobbler
{
    internal sealed class Viewer : FlowLayoutPanel, IViewer
    {
        private readonly bool _authGranted;

        private readonly bool _checkable;
        private readonly List<Entry> _entryList;
        private readonly bool _firstLoad;
        private Dictionary<string, string> _coversPath = new Dictionary<string, string>();
        private List<string> _loadingAlbums = new List<string>();
        public Mutex MutexCoversPath = new Mutex(false, "LightScrobbler.MutexCoversPath");
        public Mutex MutexLoadingAlbums = new Mutex(false, "LightScrobbler.MutexLoadingAlbums");
        public Mutex MutexThreadRequest = new Mutex(false, "LightScrobbler.ThreadRequest");

        public Viewer(List<Entry> entryList, bool checkable)
        {
            _authGranted = true;
            _checkable = checkable;
            _entryList = entryList;
            _firstLoad = true;

            foreach (Entry entry in _entryList)
            {
                var ve = new ViewerEntry(entry, _checkable);
                Controls.Add(ve);
            }

            Dock = DockStyle.Fill;
            AutoScroll = true;
            FlowDirection = FlowDirection.TopDown;
            WrapContents = true;
        }

        public bool AuthorizationGranted
        {
            get { return _authGranted; }
        }

        public List<string> LoadingAlbums
        {
            get { return _loadingAlbums; }
            set { _loadingAlbums = value; }
        }

        public Dictionary<string, string> CoversPath
        {
            get { return _coversPath; }
            set { _coversPath = value; }
        }

        #region IViewer Members

        public List<Entry> SelectedEntries
        {
            get
            {
                var entries = new List<Entry>();

                foreach (Control ctrl in Controls)
                {
                    var ve = (ViewerEntry) ctrl;

                    if (ve.Send)
                        entries.Add(ve.Entry);
                }

                return entries;
            }
        }

        public void Populate()
        {
            if (!_firstLoad) return;

            foreach (Control ctrl in Controls)
            {
                ((ViewerEntry) ctrl).StartCoverFetch(this, (_entryList.Count < 200));
            }
        }

        public void Close()
        {
            foreach (Control ctrl in Controls)
            {
                ((ViewerEntry)ctrl).JoinThread();
            }
        }

        #endregion
    }
}